package com.fzhucc.modules.dawei.service;

import com.fzhucc.utils.AESUtil;
import com.fzhucc.utils.RsaUtils2;

import java.util.HashMap;
import java.util.Map;

/**
 * ParamHandleService
 *
 * @author zhangzheng
 * @date 2023/2/3 23:19
 * desc:
 */
public class ParamHandleService {

    public static String decryptParam(String merchantPrivateKey,String gatewayPublicKey,String sign,String data,String keyPass){
        String decryptKeyPass , decryptData;
        try {
            decryptKeyPass = RsaUtils2.decrypt(merchantPrivateKey, keyPass);
            decryptData = AESUtil.decrypt(data, decryptKeyPass);
        }catch (RuntimeException e){
            return "解密失败";
        }
        boolean verify = RsaUtils2.verify(gatewayPublicKey, decryptData, sign);
        if(!verify){
            return "验签失败";
        }else{
            return decryptData;
        }
    }

    public static Map encodeParam(String merchantPrivateKey, String gatewayPublicKey, String data){
        String keyPass = AESUtil.genAesSecret();
        String encodeData = AESUtil.encodeString(data, keyPass);
        String sign = RsaUtils2.sign(merchantPrivateKey, data);
        keyPass = RsaUtils2.encrypt(gatewayPublicKey,keyPass);
        String finalKeyPass = keyPass;
        HashMap hashMap = new HashMap() {{
            put("data", encodeData);
            put("keyPass", finalKeyPass);
            put("sign", sign);
        }};
        return hashMap;
    }

//    public static void createRsaSecrteKey(){
//        Map<String, Object> keyMap;
//        try {
//            keyMap = CreateSecrteKey.initKey();
//            String publicKey1 = CreateSecrteKey.getPublicKey(keyMap);
//            String privateKey1 = CreateSecrteKey.getPrivateKey(keyMap);
//            System.out.println(publicKey1);
//            System.out.println(privateKey1);
//            System.out.println("===========================================");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    public static final String HeaderMerchantId = "merchantId";
    public static final String HeaderSign = "sign";
    public static final String HeaderKeyPass = "keyPass";

    public static void main(String[] args) {

        String merchantId = "1001";
        String merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDPhbSgqR5R0kl7HQAqMkkp7ib8cR+67RM4WJq4/chdegq5naTTXg4nVeQ2eWvkX//3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YHC9DSeWn8pHt34qOQ5PyRLC/zoqSWl62RQNxRcBikscc6e7QfnNsKIkbsQ7ow0Ql7dUBmSEnWTSxCgTMpYvnHUCFSc48U96oQKBgQCYi3PzMO1kTMBdJPwtFbIKPEcgqTVt6UQpsANFmwXPxt+cx7bHd5LgW77mtPzPugO8apA2sU63zcabKv1WO+cc2VyUMPwjI8ey6ViYW+L7m74zQalkcm8ZByZZHQk8bb5Q8+hPUjfjTZ1maIHVYa/HivVTHo9j+OLEeVJ0jgtj2wKBgEEVuWrLeMLmwJV9qXYtJUTDwAh8Zqz0a+C/W8PMx5cEkTZ8XvaznTOqMIYhfqz01OtD39HUsK5XI4md0OOyDp1rCfSYZ95IhHNPXPr3AAPnB5q/uGge2Jjxx3sl2J+Kdg09sDBHBvVPxK15DeTHmIYvkb3Myj6NioXo+AX5zciM";
        String gatewayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArkHL53wDoIj2vXX8ftiZwK3PMcjHRkLGS/oFH/3ak0iYLX9WK4ayD6erH5qM9CXjcv+YMdAQ5Cu1X3cdWD6MJIOtZk9wYyYf/dvlMBlqRgCpTGyhZvhbhl0wJ8o8Bcj5PgHDnVmVKhNiqQz4DnBYQEH3CQf2a2eEkwiveDLs2WlPNYSPXcSqDdF3CMcbeUyF3oUgxP7ZQGdRXMJ70FdS16BHy3YXSUSys/pqmv6nsnTGqbXbwVCeQB2fjTl0AW8B6rbY1gsQbiiODhDDEeQ4EjknDackBgwXThJQdAxSTR8Lkbqnsj8fg+bRSv7j+HiUBBt/tZFRP/JzLlhycakuOQIDAQAB";


        String loginJson = "{\"userAccount\":\"zhangzheng@daweisoft.com\",\"name\":\"zhangzheng\",\"company\":\"daweisoft\",\"areacode\":\"86\",\"mobilephone\":\"13600005953\",\"email\":\"13600005953@163.com\",\"password\":\"123123\",\"orgId\":\"1234\"}";

        //生成Rsa公私钥
//        createRsaSecrteKey();
        //根据自己的私钥和对方的公钥，进行参数加密加签
//        Map map = encodeParam(merchantPrivateKey, gatewayPublicKey, loginJson);
//        System.out.println(map);//data放到requestBody   keyPass、sign、merchantId放到requestHeader

        //根据自己的私钥和对方的公钥，进行参数解密验签
        //data从responseBody中取（需要判断code=0）   keyPass和sign从responseHeader中取
        String data = "btXsldXsVRx+oT27KQkq1YjJPxk5sSZb/jLlnWzIcRAayQDxw5twq7yxPSg8k9JTd6Ycv2Q+wToFcudJIpPJ2Im1CIadAWoIAnTMhTdqsOtc56rkRF49aEdwnks/zjN6EyGWRHoaDgdhcYwOELmA+ScwXl4soUTdI/vW/r5YyS8XcAUmcluNjmaa4zd8JRYEV6NisQw6ljxoQGjsBbEzm8Rk3/+Qsp3+S7d9d0ddPRtoHyG+1n8qpaM7I0F/+BuD8/Pw7R2BKtMJSrSStsdsTUi5ztL7BCJ+gNNdWsqMBxc=";
        String keyPass = "OHJ8O27Twz4PNCur85UNPgxp6MgLNVBktvpBGmknmu6PcHKAkydSY13uAawfJPyh/CLOwhRFmygnet9pynREherHRGm0W7ZFRAanAlSDKkGQGFHZiqdITFDsEYoIJJ+FwoIKzh0nskh4MsOd7+8LIkdZvdcqMrhKN7q4uMyU7ezIdiUyhNK59eGPP/SATQW88InqgX/Dg7SEY99wAPnGAGJzSp4m156thnyI+TmYvM3K70Dccqe0CzMZ2tVr5qJj5m0lRIdzL2xgpakBYTj//1Uq78c5wHjSzZ+YkqZpHMKHdJf7/Cpv3pYSt20JEiVk9OUWpCa8A3sm0Na8QO+ffw==";
        String sign = "TQdeWBQEGeWiDlJXT9szONw0HTbx1PrrLKwA3YvUCIuRdhcBMhbpXZdum7vlY2b5FWRThOdhzXVZcWMcycfxqNxDE48BPy8m5Q+uRFzK0vr8TL2TOiJb0TqS7XFWVJPa8ZMzAM3KBsFB7dqeqIUpbyVikx0fHA9cMBvGz+X9EsXES0Y5ZiGkavXJkmKQ9fT5RTQ09QVzC8OSf90/cmwxVcqUc1gYElLXmCtj5kKas8ICEvzd972pAGh4/STKWCEOzK0C7+IhPSTwUlWWMRVIMRp5jDirjesEEsqeUNrAReHS7YRj/lk6t+tfL+nFq2rYrASOEsrzi2VMoUgyYmKd5g==";
        String param = decryptParam(merchantPrivateKey, gatewayPublicKey, sign, data, keyPass);
        System.out.println(param);
    }

}
